package com.zp.base.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.base.entity.bo.ClinicItemDictBo;
import com.zp.base.entity.vo.ClinicItemDictVo;
import com.zp.base.service.IClinicItemDictService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 诊疗项目控制器
 * 前端访问路由地址为:/base/clinicItemDict
 *
 * @author zhang peng
 * @date 2023-01-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/clinicItemDict")
public class ClinicItemDictController extends BaseController {

    private final IClinicItemDictService iClinicItemDictService;

    /**
     * 查询诊疗项目列表 (分页)
     */
    @SaCheckPermission("base:clinicItemDict:list")
    @GetMapping("/selectPage")
    public TableDataInfo<ClinicItemDictVo> selectPage (ClinicItemDictBo bo, PageQuery pageQuery) {
        return iClinicItemDictService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询诊疗项目列表 (不分页) || 诊疗项目自动补全
     */
//    @SaCheckPermission("base:clinicItemDict:list")
    @GetMapping("/list")
    public R<List<ClinicItemDictVo>> list (ClinicItemDictBo bo) {
        List<ClinicItemDictVo> list = iClinicItemDictService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出诊疗项目列表
     */
    @SaCheckPermission("base:clinicItemDict:export")
    @Log(title = "诊疗项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export (ClinicItemDictBo bo, HttpServletResponse response) {
        List<ClinicItemDictVo> list = iClinicItemDictService.queryList(bo);
        ExcelUtil.exportExcel(list, "诊疗项目", ClinicItemDictVo.class, response);
    }

    /**
     * 获取诊疗项目详细信息
     *
     * @param clinicItemId 主键
     */
    @SaCheckPermission("base:clinicItemDict:query")
    @GetMapping("/{clinicItemId}")
    public R<ClinicItemDictVo> getInfo (@NotNull(message = "主键不能为空") @PathVariable String clinicItemId) {
        return R.ok(iClinicItemDictService.queryById(clinicItemId));
    }

    /**
     * 新增诊疗项目
     */
    @SaCheckPermission("base:clinicItemDict:add")
    @Log(title = "诊疗项目", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add (@Validated(AddGroup.class) @RequestBody ClinicItemDictBo bo) {
        return toAjax(iClinicItemDictService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改诊疗项目
     */
    @SaCheckPermission("base:clinicItemDict:edit")
    @Log(title = "诊疗项目", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit (@Validated(EditGroup.class) @RequestBody ClinicItemDictBo bo) {
        return toAjax(iClinicItemDictService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除诊疗项目
     *
     * @param clinicItemIds 主键串
     */
    @SaCheckPermission("base:clinicItemDict:remove")
    @Log(title = "诊疗项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{clinicItemIds}")
    public R<Void> remove (@NotEmpty(message = "主键不能为空") @PathVariable String[] clinicItemIds) {
        return toAjax(iClinicItemDictService.deleteWithValidByIds(Arrays.asList(clinicItemIds), true) ? 1 : 0);
    }
}
